package sa4project1

import java.net.URL;
import scala.xml._
import parsing._
import scala.collection.mutable
import scala.xml.XML._

object P1_ex2 {

 
  def main(args: Array[String]): Unit = {
    val query = args(0);
    val twitter = "http://search.twitter.com/search.atom?q=" + query + "&rpp=100";
    
    val url = new URL(twitter);
    val conn = url.openConnection;
    val xml = XML.load(conn.getInputStream);
    
    var how_many_tweet = 0;
    
    val authors = mutable.Set("");
    authors.clear;
    
    for (res <- (xml \\ "entry")){
      if(!authors.contains((res \\ "author").text)){
        authors.add((res \\ "author").text);
      }
      how_many_tweet += 1;
    }
    
    var page : scala.xml.Elem = 
      <html>
        <head>
          <title>Twitter Feed Viewer</title>
        </head>
        <body>
          <h1>Software Atelier 4 - Project 1</h1>
          <h2>{"The query " + query + " matched " + how_many_tweet + " tweets by " + authors.size + " users."}</h2>
          <table border="1">
            <tr>
              <th scope="col">Tweet</th>
              <th scope="col">Author</th>
              <th scope="col">Date</th>
              <th scope="col">Google Map</th>

            </tr>
            {for (res <- (xml \\ "entry"))
              yield 
                <tr>
                  <td>
                    {
                      // Small hack for wrong tags encoding
                      var string_plain = (res \\ "content").toString
                      string_plain = string_plain.replaceAll("\\&lt;","<");
                      string_plain = string_plain.replaceAll("\\&quot;","\"");
                      string_plain = string_plain.replaceAll("\\&gt;",">");
                      val new_xml = XML.loadString(string_plain);
                      new_xml
                    }
                  </td>
                  <td><a href={""+ (res \\ "author" \\ "uri").text}> {(res \\ "author" \\ "name").text} </a></td>
                  <td>
                    {
                      val df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                      df.setTimeZone(java.util.TimeZone.getTimeZone("Central European Time"))
                      val date = df.parse((res \\ "published").text)
                      date
                      }
                  </td>
                  <td>
                    {
                      val position = (res \\ "point").text
                      if (position == ""){
                        <b>No maps</b>
                      } else {
                        val string_pos = (position.toString).replaceAll(" ",",");
                        val google_maps_url = "http://maps.google.com/maps/api/staticmap?center="+ string_pos +"&zoom=12&size=250x250&sensor=false";
                        <img src={google_maps_url}></img>
                        
                      }
                    }
                  </td>
                </tr>                
            }
          </table>
        </body>
      </html>;
        
    save("exercise2.html", page, "UTF-8")
    println(page)

 
  }
}
